<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>forms.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_forms.js.html">forms.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for manipulating a form and elements.
<a name="line17"></a> *
<a name="line18"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line19"></a> * @author jonp@google.com (Jon Perlow)
<a name="line20"></a> * @author elsigh@google.com (Lindsey Simon)
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.dom.forms&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Returns form data as a map of name to value arrays. This doesn&#39;t
<a name="line30"></a> * support file inputs.
<a name="line31"></a> * @param {HTMLFormElement} form The form.
<a name="line32"></a> * @return {!goog.structs.Map} A map of the form data as form name to arrays of
<a name="line33"></a> *     values.
<a name="line34"></a> */
<a name="line35"></a>goog.dom.forms.getFormDataMap = function(form) {
<a name="line36"></a>  var map = new goog.structs.Map();
<a name="line37"></a>  goog.dom.forms.getFormDataHelper_(form, map,
<a name="line38"></a>      goog.dom.forms.addFormDataToMap_);
<a name="line39"></a>  return map;
<a name="line40"></a>};
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Returns the form data as an application/x-www-url-encoded string. This
<a name="line45"></a> * doesn&#39;t support file inputs.
<a name="line46"></a> * @param {HTMLFormElement} form The form.
<a name="line47"></a> * @return {string} An application/x-www-url-encoded string.
<a name="line48"></a> */
<a name="line49"></a>goog.dom.forms.getFormDataString = function(form) {
<a name="line50"></a>  var sb = [];
<a name="line51"></a>  goog.dom.forms.getFormDataHelper_(form, sb,
<a name="line52"></a>      goog.dom.forms.addFormDataToStringBuffer_);
<a name="line53"></a>  return sb.join(&#39;&amp;&#39;);
<a name="line54"></a>};
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Returns the form data as a map or an application/x-www-url-encoded
<a name="line59"></a> * string. This doesn&#39;t support file inputs.
<a name="line60"></a> * @param {HTMLFormElement} form The form.
<a name="line61"></a> * @param {Object} result The object form data is being put in.
<a name="line62"></a> * @param {Function} fnAppend Function that takes {@code result}, an element
<a name="line63"></a> *     name, and an element value, and adds the name/value pair to the result
<a name="line64"></a> *     object.
<a name="line65"></a> * @private
<a name="line66"></a> */
<a name="line67"></a>goog.dom.forms.getFormDataHelper_ = function(form, result, fnAppend) {
<a name="line68"></a>  var els = form.elements;
<a name="line69"></a>  for (var el, i = 0; el = els[i]; i++) {
<a name="line70"></a>    if (// Make sure we don&#39;t include elements that are not part of the form.
<a name="line71"></a>        // Some browsers include non-form elements. Check for &#39;form&#39; property.
<a name="line72"></a>        // See http://code.google.com/p/closure-library/issues/detail?id=227
<a name="line73"></a>        // and
<a name="line74"></a>        // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#the-input-element
<a name="line75"></a>        (el.form != form) ||
<a name="line76"></a>        el.disabled ||
<a name="line77"></a>        // HTMLFieldSetElement has a form property but no value.
<a name="line78"></a>        el.tagName.toLowerCase() == &#39;fieldset&#39;) {
<a name="line79"></a>      continue;
<a name="line80"></a>    }
<a name="line81"></a>
<a name="line82"></a>    var name = el.name;
<a name="line83"></a>    switch (el.type.toLowerCase()) {
<a name="line84"></a>      case &#39;file&#39;:
<a name="line85"></a>        // file inputs are not supported
<a name="line86"></a>      case &#39;submit&#39;:
<a name="line87"></a>      case &#39;reset&#39;:
<a name="line88"></a>      case &#39;button&#39;:
<a name="line89"></a>        // don&#39;t submit these
<a name="line90"></a>        break;
<a name="line91"></a>      case &#39;select-multiple&#39;:
<a name="line92"></a>        var values = goog.dom.forms.getValue(el);
<a name="line93"></a>        if (values != null) {
<a name="line94"></a>          for (var value, j = 0; value = values[j]; j++) {
<a name="line95"></a>            fnAppend(result, name, value);
<a name="line96"></a>          }
<a name="line97"></a>        }
<a name="line98"></a>        break;
<a name="line99"></a>      default:
<a name="line100"></a>        var value = goog.dom.forms.getValue(el);
<a name="line101"></a>        if (value != null) {
<a name="line102"></a>          fnAppend(result, name, value);
<a name="line103"></a>        }
<a name="line104"></a>    }
<a name="line105"></a>  }
<a name="line106"></a>
<a name="line107"></a>  // input[type=image] are not included in the elements collection
<a name="line108"></a>  var inputs = form.getElementsByTagName(&#39;input&#39;);
<a name="line109"></a>  for (var input, i = 0; input = inputs[i]; i++) {
<a name="line110"></a>    if (input.form == form &amp;&amp; input.type.toLowerCase() == &#39;image&#39;) {
<a name="line111"></a>      name = input.name;
<a name="line112"></a>      fnAppend(result, name, input.value);
<a name="line113"></a>      fnAppend(result, name + &#39;.x&#39;, &#39;0&#39;);
<a name="line114"></a>      fnAppend(result, name + &#39;.y&#39;, &#39;0&#39;);
<a name="line115"></a>    }
<a name="line116"></a>  }
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Adds the name/value pair to the map.
<a name="line122"></a> * @param {goog.structs.Map} map The map to add to.
<a name="line123"></a> * @param {string} name The name.
<a name="line124"></a> * @param {string} value The value.
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.dom.forms.addFormDataToMap_ = function(map, name, value) {
<a name="line128"></a>  var array = map.get(name);
<a name="line129"></a>  if (!array) {
<a name="line130"></a>    array = [];
<a name="line131"></a>    map.set(name, array);
<a name="line132"></a>  }
<a name="line133"></a>  array.push(value);
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Adds a name/value pair to an string buffer array in the form &#39;name=value&#39;.
<a name="line139"></a> * @param {Array} sb The string buffer array for storing data.
<a name="line140"></a> * @param {string} name The name.
<a name="line141"></a> * @param {string} value The value.
<a name="line142"></a> * @private
<a name="line143"></a> */
<a name="line144"></a>goog.dom.forms.addFormDataToStringBuffer_ = function(sb, name, value) {
<a name="line145"></a>  sb.push(encodeURIComponent(name) + &#39;=&#39; + encodeURIComponent(value));
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Whether the form has a file input.
<a name="line151"></a> * @param {HTMLFormElement} form The form.
<a name="line152"></a> * @return {boolean} Whether the form has a file input.
<a name="line153"></a> */
<a name="line154"></a>goog.dom.forms.hasFileInput = function(form) {
<a name="line155"></a>  var els = form.elements;
<a name="line156"></a>  for (var el, i = 0; el = els[i]; i++) {
<a name="line157"></a>    if (!el.disabled &amp;&amp; el.type &amp;&amp; el.type.toLowerCase() == &#39;file&#39;) {
<a name="line158"></a>      return true;
<a name="line159"></a>    }
<a name="line160"></a>  }
<a name="line161"></a>  return false;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Enables or disables either all elements in a form or a single form element.
<a name="line167"></a> * @param {Element} el The element, either a form or an element within a form.
<a name="line168"></a> * @param {boolean} disabled Whether the element should be disabled.
<a name="line169"></a> */
<a name="line170"></a>goog.dom.forms.setDisabled = function(el, disabled) {
<a name="line171"></a>  // disable all elements in a form
<a name="line172"></a>  if (el.tagName == &#39;FORM&#39;) {
<a name="line173"></a>    var els = el.elements;
<a name="line174"></a>    for (var i = 0; el = els[i]; i++) {
<a name="line175"></a>      goog.dom.forms.setDisabled(el, disabled);
<a name="line176"></a>    }
<a name="line177"></a>  } else {
<a name="line178"></a>    // makes sure to blur buttons, multi-selects, and any elements which
<a name="line179"></a>    // maintain keyboard/accessibility focus when disabled
<a name="line180"></a>    if (disabled == true) {
<a name="line181"></a>      el.blur();
<a name="line182"></a>    }
<a name="line183"></a>    el.disabled = disabled;
<a name="line184"></a>  }
<a name="line185"></a>};
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * Focuses, and optionally selects the content of, a form element.
<a name="line190"></a> * @param {Element} el The form element.
<a name="line191"></a> */
<a name="line192"></a>goog.dom.forms.focusAndSelect = function(el) {
<a name="line193"></a>  el.focus();
<a name="line194"></a>  if (el.select) {
<a name="line195"></a>    el.select();
<a name="line196"></a>  }
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Whether a form element has a value.
<a name="line202"></a> * @param {Element} el The element.
<a name="line203"></a> * @return {boolean} Whether the form has a value.
<a name="line204"></a> */
<a name="line205"></a>goog.dom.forms.hasValue = function(el) {
<a name="line206"></a>  var value = goog.dom.forms.getValue(el);
<a name="line207"></a>  return !!value;
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * Whether a named form field has a value.
<a name="line213"></a> * @param {HTMLFormElement} form The form element.
<a name="line214"></a> * @param {string} name Name of an input to the form.
<a name="line215"></a> * @return {boolean} Whether the form has a value.
<a name="line216"></a> */
<a name="line217"></a>goog.dom.forms.hasValueByName = function(form, name) {
<a name="line218"></a>  var value = goog.dom.forms.getValueByName(form, name);
<a name="line219"></a>  return !!value;
<a name="line220"></a>};
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * Gets the current value of any element with a type.
<a name="line225"></a> * @param {Element} el The element.
<a name="line226"></a> * @return {string|Array.&lt;string&gt;|null} The current value of the element
<a name="line227"></a> *     (or null).
<a name="line228"></a> */
<a name="line229"></a>goog.dom.forms.getValue = function(el) {
<a name="line230"></a>  var type = el.type;
<a name="line231"></a>  if (!goog.isDef(type)) {
<a name="line232"></a>    return null;
<a name="line233"></a>  }
<a name="line234"></a>  switch (type.toLowerCase()) {
<a name="line235"></a>    case &#39;checkbox&#39;:
<a name="line236"></a>    case &#39;radio&#39;:
<a name="line237"></a>      return goog.dom.forms.getInputChecked_(el);
<a name="line238"></a>    case &#39;select-one&#39;:
<a name="line239"></a>      return goog.dom.forms.getSelectSingle_(el);
<a name="line240"></a>    case &#39;select-multiple&#39;:
<a name="line241"></a>      return goog.dom.forms.getSelectMultiple_(el);
<a name="line242"></a>    default:
<a name="line243"></a>      return goog.isDef(el.value) ? el.value : null;
<a name="line244"></a>  }
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * Alias for goog.dom.form.element.getValue
<a name="line250"></a> * @type {Function}
<a name="line251"></a> * @deprecated Use {@link goog.dom.forms.getValue} instead.
<a name="line252"></a> */
<a name="line253"></a>goog.dom.$F = goog.dom.forms.getValue;
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Returns the value of the named form field. In the case of radio buttons,
<a name="line258"></a> * returns the value of the checked button with the given name.
<a name="line259"></a> *
<a name="line260"></a> * @param {HTMLFormElement} form The form element.
<a name="line261"></a> * @param {string} name Name of an input to the form.
<a name="line262"></a> *
<a name="line263"></a> * @return {Array.&lt;string&gt;|string|null} The value of the form element, or
<a name="line264"></a> *     null if the form element does not exist or has no value.
<a name="line265"></a> */
<a name="line266"></a>goog.dom.forms.getValueByName = function(form, name) {
<a name="line267"></a>  var els = form.elements[name];
<a name="line268"></a>
<a name="line269"></a>  if (els.type) {
<a name="line270"></a>    return goog.dom.forms.getValue(els);
<a name="line271"></a>  } else {
<a name="line272"></a>    for (var i = 0; i &lt; els.length; i++) {
<a name="line273"></a>      var val = goog.dom.forms.getValue(els[i]);
<a name="line274"></a>      if (val) {
<a name="line275"></a>        return val;
<a name="line276"></a>      }
<a name="line277"></a>    }
<a name="line278"></a>
<a name="line279"></a>    return null;
<a name="line280"></a>  }
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Gets the current value of a checkable input element.
<a name="line286"></a> * @param {Element} el The element.
<a name="line287"></a> * @return {?string} The value of the form element (or null).
<a name="line288"></a> * @private
<a name="line289"></a> */
<a name="line290"></a>goog.dom.forms.getInputChecked_ = function(el) {
<a name="line291"></a>  return el.checked ? el.value : null;
<a name="line292"></a>};
<a name="line293"></a>
<a name="line294"></a>
<a name="line295"></a>/**
<a name="line296"></a> * Gets the current value of a select-one element.
<a name="line297"></a> * @param {Element} el The element.
<a name="line298"></a> * @return {?string} The value of the form element (or null).
<a name="line299"></a> * @private
<a name="line300"></a> */
<a name="line301"></a>goog.dom.forms.getSelectSingle_ = function(el) {
<a name="line302"></a>  var selectedIndex = el.selectedIndex;
<a name="line303"></a>  return selectedIndex &gt;= 0 ? el.options[selectedIndex].value : null;
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * Gets the current value of a select-multiple element.
<a name="line309"></a> * @param {Element} el The element.
<a name="line310"></a> * @return {Array.&lt;string&gt;?} The value of the form element (or null).
<a name="line311"></a> * @private
<a name="line312"></a> */
<a name="line313"></a>goog.dom.forms.getSelectMultiple_ = function(el) {
<a name="line314"></a>  var values = [];
<a name="line315"></a>  for (var option, i = 0; option = el.options[i]; i++) {
<a name="line316"></a>    if (option.selected) {
<a name="line317"></a>      values.push(option.value);
<a name="line318"></a>    }
<a name="line319"></a>  }
<a name="line320"></a>  return values.length ? values : null;
<a name="line321"></a>};
<a name="line322"></a>
<a name="line323"></a>
<a name="line324"></a>/**
<a name="line325"></a> * Sets the current value of any element with a type.
<a name="line326"></a> * @param {Element} el The element.
<a name="line327"></a> * @param {*=} opt_value The value to give to the element, which will be coerced
<a name="line328"></a> *     by the browser in the default case using toString. This value should be
<a name="line329"></a> *     an array for setting the value of select multiple elements.
<a name="line330"></a> */
<a name="line331"></a>goog.dom.forms.setValue = function(el, opt_value) {
<a name="line332"></a>  var type = el.type;
<a name="line333"></a>  if (goog.isDef(type)) {
<a name="line334"></a>    switch (type.toLowerCase()) {
<a name="line335"></a>      case &#39;checkbox&#39;:
<a name="line336"></a>      case &#39;radio&#39;:
<a name="line337"></a>        goog.dom.forms.setInputChecked_(el,
<a name="line338"></a>            /** @type {string} */ (opt_value));
<a name="line339"></a>        break;
<a name="line340"></a>      case &#39;select-one&#39;:
<a name="line341"></a>        goog.dom.forms.setSelectSingle_(el,
<a name="line342"></a>            /** @type {string} */ (opt_value));
<a name="line343"></a>        break;
<a name="line344"></a>      case &#39;select-multiple&#39;:
<a name="line345"></a>        goog.dom.forms.setSelectMultiple_(el,
<a name="line346"></a>            /** @type {Array} */ (opt_value));
<a name="line347"></a>        break;
<a name="line348"></a>      default:
<a name="line349"></a>        el.value = goog.isDefAndNotNull(opt_value) ? opt_value : &#39;&#39;;
<a name="line350"></a>    }
<a name="line351"></a>  }
<a name="line352"></a>};
<a name="line353"></a>
<a name="line354"></a>
<a name="line355"></a>/**
<a name="line356"></a> * Sets a checkable input element&#39;s checked property.
<a name="line357"></a> * #TODO(user): This seems potentially unintuitive since it doesn&#39;t set
<a name="line358"></a> * the value property but my hunch is that the primary use case is to check a
<a name="line359"></a> * checkbox, not to reset its value property.
<a name="line360"></a> * @param {Element} el The element.
<a name="line361"></a> * @param {string|boolean=} opt_value The value, sets the element checked if
<a name="line362"></a> *     val is set.
<a name="line363"></a> * @private
<a name="line364"></a> */
<a name="line365"></a>goog.dom.forms.setInputChecked_ = function(el, opt_value) {
<a name="line366"></a>  el.checked = opt_value ? &#39;checked&#39; : null;
<a name="line367"></a>};
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>/**
<a name="line371"></a> * Sets the value of a select-one element.
<a name="line372"></a> * @param {Element} el The element.
<a name="line373"></a> * @param {string=} opt_value The value of the selected option element.
<a name="line374"></a> * @private
<a name="line375"></a> */
<a name="line376"></a>goog.dom.forms.setSelectSingle_ = function(el, opt_value) {
<a name="line377"></a>  // unset any prior selections
<a name="line378"></a>  el.selectedIndex = -1;
<a name="line379"></a>  if (goog.isString(opt_value)) {
<a name="line380"></a>    for (var option, i = 0; option = el.options[i]; i++) {
<a name="line381"></a>      if (option.value == opt_value) {
<a name="line382"></a>        option.selected = true;
<a name="line383"></a>        break;
<a name="line384"></a>      }
<a name="line385"></a>    }
<a name="line386"></a>  }
<a name="line387"></a>};
<a name="line388"></a>
<a name="line389"></a>
<a name="line390"></a>/**
<a name="line391"></a> * Sets the value of a select-multiple element.
<a name="line392"></a> * @param {Element} el The element.
<a name="line393"></a> * @param {Array.&lt;string&gt;|string=} opt_value The value of the selected option
<a name="line394"></a> *     element(s).
<a name="line395"></a> * @private
<a name="line396"></a> */
<a name="line397"></a>goog.dom.forms.setSelectMultiple_ = function(el, opt_value) {
<a name="line398"></a>  // reset string opt_values as an array
<a name="line399"></a>  if (goog.isString(opt_value)) {
<a name="line400"></a>    opt_value = [opt_value];
<a name="line401"></a>  }
<a name="line402"></a>  for (var option, i = 0; option = el.options[i]; i++) {
<a name="line403"></a>    // we have to reset the other options to false for select-multiple
<a name="line404"></a>    option.selected = false;
<a name="line405"></a>    if (opt_value) {
<a name="line406"></a>      for (var value, j = 0; value = opt_value[j]; j++) {
<a name="line407"></a>        if (option.value == value) {
<a name="line408"></a>          option.selected = true;
<a name="line409"></a>        }
<a name="line410"></a>      }
<a name="line411"></a>    }
<a name="line412"></a>  }
<a name="line413"></a>};
</pre>


</body>
</html>
